package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.SecurityTrainingTypeBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.service.SecurityTrainingTypeService;

@Controller("securityTrainingTypeAction")
@Scope("prototype")
public class SecurityTrainingTypeAction extends BaseAction<SecurityTrainingTypeBean> {

	private static final long serialVersionUID = 19802742983894850L;
	private static Log log = LogFactory.getLog(SecurityTrainingTypeAction.class);
	
	private SecurityTrainingTypeService securityTrainingTypeService;

	@Autowired
	public void setSecurityTrainingTypeService(
			SecurityTrainingTypeService securityTrainingTypeService) {
		this.securityTrainingTypeService = securityTrainingTypeService;
	}
	
	public String findAll() {
		try {
			List<SecurityTrainingTypeBean> securityTrainingTypeList = securityTrainingTypeService.getListSecurityTrainingType(null);
			entity.setAllList(securityTrainingTypeList);
			entity.setList(securityTrainingTypeList.subList(entity.getCurrentRow(), entity.getLastRow()));
			
			this.button("Super.Popup(5,'添加','/SCMDB/securityTrainingTypeAction!newSecurityTrainingType.action','',250,150)",
					"Super.editData('/SCMDB/securityTrainingTypeAction!findSecurityTrainingType.action?id=',250,150)",
					"Super.delData('/SCMDB/securityTrainingTypeAction!delSecurityTrainingType.action?pId=')");
		} catch (Exception e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		}
		
		return SUCCESS;
	}
	
	public String findSecurityTrainingType() {
		try {
			String pId = request.getParameter("id");
			entity = securityTrainingTypeService.getSecurityTrainingType(pId);
			return "editSucc";
		} catch (Exception e) {
			log.error("find securityTrainingType failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	
	public String newSecurityTrainingType() {
		return "newSucc";
	}
	
	public String addSecurityTrainingType() {
		try {
			String result = securityTrainingTypeService.createSecurityTrainingType(entity);
			messageSession(result, "安全培训交底类型");
			return "commitSucc";
		} catch (Exception e) {
			log.error("add securityTrainingType failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	
	public String editSecurityTrainingType() {
		try {
			String result = securityTrainingTypeService.editSecurityTrainingType(entity);
			messageSession(result, "安全培训交底类型");
			return "commitSucc";
		} catch (Exception e) {
			log.error("edit securityTrainingType failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	
	public String delSecurityTrainingType() {
		String flg = null;
		try {
			String pId = request.getParameter("pId");
			if (StringUtils.isNotEmpty(pId)) {
				String[] arr = pId.split(",");
				flg = securityTrainingTypeService.delSecurityTrainingType(arr);
			}
			
			messageSession(flg, "安全培训交底类型");
			log.info("delete suncce");
			return "commitSucc";
		} catch (Exception e) {
			log.error("delete securityTrainingType fail" + e.getMessage());
			return "fail";
		}		
	}
}
